package com.funo.main.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.springframework.stereotype.Repository;

import com.funo.common.pojo.QueryCondition;
import com.funo.core.base.BaseDao;
import com.funo.main.dao.OrgDao;
import com.funo.main.entities.Org;

/**
 * 机构管理接口实现类
 * 
 * @author zhengjs
 *
 */
@Repository
public class OrgDaoImpl extends BaseDao<Org, String> implements OrgDao{

	@Override
	public List<Org> getMenu() {
		return findByHql("from Org where parentOrg = null order by orderNum");
	}

	@Override
	public List<Org> getMenu(String id) {
		return findByHql("from Org where parentOrg.id = ? order by orderNum",id);
	}

	@Override
	public Integer count(QueryCondition condition) {
		Criteria criteria = super.getCriteria();
		return super.countByCriteria(criteria);
	}

	@Override
	public List<Org> pageList(QueryCondition condition) {
		Criteria criteria = super.getCriteria();
		criteria.setFirstResult(condition.getStart());
		criteria.setMaxResults(condition.getLimit());
		return super.findByCriteria(criteria);
	}

	@Override
	public Org getOrg(String name) {
		List<Org> list = super.findByHql("from Org where name=?", name);
		return list.size()==1?list.get(0):null;
	}

	@Override
	public Org findOrgByName(String name, String id) {
		
		String hql ="from Org where name = ? and id !=?";

		List<Org> list =super.findByHql(hql, name, id);
		
		return list!=null && list.size()>0?list.get(0):null;
	}

}
